<template>
	<view>
		<view class="top-box" :style="'height:'+navHeight+'rpx;'">
			<view class="search-view" style="bottom: 80rpx;">
				<text class="icon fanhui" @click="goBack"></text>
				<picker @change="searchTypeChange" :value="searchType" :range="searchTypes">
					<view class="picker">{{searchTypes[searchType]}}</view>
				</picker>
				<input class="ser-input" confirm-type="search" :value="keyword" @confirm="search" placeholder="快速搜索商标名" />
			</view>
			<view class="navbar">
				<view class="nav-item" :class="{current: cate > 0}">
					<picker @change="cateChange" :value="cate" :range="trademarkCates">
						<view class="picker">{{trademarkCates[cate]}}</view>
					</picker>
				</view>
				<view class="nav-item" :class="{current: status > 0}">
					<picker @change="statusChange" :value="status" :range="trademarkStatus">
						<view class="picker">{{trademarkStatus[status]}}</view>
					</picker>
				</view>
			</view>
		</view>
		<view class="search-list" :style="'margin-top:'+navHeight+'rpx;'">
			<block v-if="list.length > 0">
				<view v-for="(item, index) in list" :key="index" class="search-item">
					<view class="pic" @click="goDetail(item.applyNo)">
						<image :src="item.pic" mode="aspectFit" lazy-load="true" @error="$imageError(item)"></image>
					</view>
					<view class="status">{{trademarkStatus[item.status]}}</view>
					<view class="title" @click="goDetail(item.applyNo)">
						{{item.name}}
					</view>
					<view class="info">
						<view class="number" @click="$copyData(item.applyNo)">注册号:{{item.applyNo}}</view>
						<view class="bigcate">{{item.cate}}类</view>
					</view>
				</view>
			</block>
			<view v-else class="empty">
				<view class="icon hezi"></view>
				<view>暂无数据</view>
			</view>
		</view>
		<view v-if="list.length > 6" class="gotop">
			<button class="gotop-btn" @click="goTop"><text class="icon xiangshang"></text></button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				navHeight: 0,
				searchTypes: ['近似', '注册号', '申请人', '机构'],
				types: ['GENERAL', 'APPLYNO', 'APPLICANT', 'AGENCY'],
				searchType: 0,
				config: {},
				trademarkStatus: getApp().globalData.trademarkStatus,
				trademarkCates: getApp().globalData.trademarkCates,
				cate: 0,
				status: 0,
				keyword: '',
				list: [],
				page: 1,
				hasMore: true
			}
		},
		onPageScroll({scrollTop}) {
			this.scrollTop = scrollTop
		},
		onLoad(e) {
			let that = this
			uni.getSystemInfo({
				success: function(res) {
					that.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 160
				}
			})
			this.searchType = e.searchType ? e.searchType : 0
			this.keyword = e.keyword
			this.page = 1
			this.create()
		},
		//加载更多
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.loadData()
			}
		},
		methods: {
			async create() {
				this.config = await this.$api('GET', 'trademark/create')
				if (this.keyword) {
					this.loadData('refresh')
				}
			},
			goDetail(applyNo) {
				this.$goto('/pages/trademark/detail?applyNo=' + applyNo)
			},
			goBack() {
				let current = getCurrentPages()
				if (current.length > 1) {
					uni.navigateBack()
				} else {
					uni.reLaunch({
						url: '/pages/tabBar1'
					})
				}
			},
			searchTypeChange(e) {
				this.searchType = e.target.value
			},
			cateChange(e) {
				this.cate = e.target.value
				this.page = 1
				this.goTop()
				this.loadData('refresh')
			},
			statusChange(e) {
				this.status = e.target.value
				this.page = 1
				this.goTop()
				this.loadData('refresh')
			},
			search(e) {
				this.keyword = e.detail.value
				if (this.keyword) {
					this.status = 0
					this.cate = 0
					this.page = 1
					this.goTop()
					this.loadData('refresh')
				} else {
					uni.showModal({
						title: '提示',
						content: '请输入关键词!',
						showCancel: false
					})
				}
			},
			async loadData(action = 'add') {
				if (action == 'refresh') {
					this.list = []
				}
				let param = JSON.parse(JSON.stringify(this.config.param))
				for (let key in param) {
					if (param[key] == 'cate') {
						param[key] = this.cate > 0 ? this.cate : ''
					} else if (param[key] == 'status') {
						param[key] = this.status > 0 ? this.trademarkStatus[this.status] : ''
					} else if (param[key] == 'type') {
						param[key] = this.types[this.searchType]
					} else if (param[key] == 'keyword') {
						param[key] = this.keyword
					} else if (param[key] == 'page') {
						param[key] = this.page
					}
				}
				let data = await this.$http('POST', 'https://testapi.qifufu.com/zzl-qccip/v3/query/queryByType', param,this.config.header)
				let list = await this.$api('POST', 'trademark', {data: data.data[this.config.data]})
				this.list = this.list.concat(list)
				if (list.length < param.pageSize) {
					this.hasMore = false
				} else {
					this.hasMore = true
				}
			},
			goTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				})
			}
		}
	}
</script>

<style lang="scss">
	.top-box {
		background: #fff;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 100;
		width: 100%;
		border-bottom: 1rpx solid #eee;

		.search-view {
			display: flex;
			position: absolute;
			bottom: 0rpx;
			padding: 10rpx;

			.fanhui {
				font-weight: bold;
				text-align: center;
				width: 60rpx;
				height: 60rpx;
				font-size: 42rpx;
				line-height: 60rpx;
			}

			.picker {
				position: absolute;
				width: 130rpx;
				height: 60rpx;
				font-size: 28rpx;
				line-height: 60rpx;
				text-align: center;
				color: #fff;
				background: $uni-color-primary;
				border-radius: 25rpx 0 0 25rpx;
				z-index: 2;

				&:after {
					padding-left: 10upx;
					content: '▾';
				}
			}

			.ser-input {
				width: 450rpx;
				height: 60rpx;
				line-height: 60rpx;
				text-align: left;
				font-size: 28rpx;
				color: $font-color-base;
				border-radius: 25rpx;
				background: rgba(230, 230, 230, .6);
				padding-left: 140rpx;
			}
		}

		.navbar {
			position: absolute;
			bottom: 0rpx;
			display: flex;
			width: 100%;
			height: 80rpx;
			background: #fff;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, .06);

			.nav-item {
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				height: 100%;
				font-size: 30rpx;
				color: #666;
				position: relative;

				&:after {
					padding-left: 15rpx;
					content: '▾';
				}

				&.current {
					color: $uni-color-primary;

					&:after {
						content: '';
						position: absolute;
						left: 50%;
						bottom: 0;
						transform: translateX(-50%);
						width: 80%;
						height: 0;
						border-bottom: 5rpx solid $uni-color-primary;
					}
				}
			}
		}
	}

	.search-list {
		display: flex;
		flex-wrap: wrap;
		height: 100%;

		.search-item {
			position: relative;
			background: #fff;
			float: left;
			width: 46%;
			padding: 1%;
			margin: 1%;
			border-radius: 10upx;
		}

		.pic {
			width: 100%;
			height: 230upx;
			border-radius: 3px;
			overflow: hidden;
			.easy-loadimage{
				height: 230upx;
			}
			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			margin: 8upx 0;
			font-size: $font-sm+4upx;
			color: #333;
			letter-spacing: 0;
			display: -webkit-box;
			overflow: hidden;
			text-overflow: ellipsis;
			word-wrap: break-word;
			white-space: normal !important;
			-webkit-line-clamp: 1;
			-webkit-box-orient: vertical;
		}

		.info {
			margin: 8upx 5upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.number {
			font-size: $font-sm;
			color: $uni-color-primary;
			line-height: 1;
		}

		.status {
			height: 40upx;
			line-height: 40upx;
			text-align: center;
			font-size: $font-sm;
			background: #999;
			color: #fff;
		}
	}
</style>
